package cn.com.customer.manager.ui.message.msg


import android.os.Bundle
import cn.com.customer.manager.R
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.bean.*
import cn.com.customer.manager.http.BaseApi
import cn.com.customer.manager.http.subscriber.TipRequestSubscriber
import com.xuexiang.xhttp2.XHttp
import com.xuexiang.xhttp2.XHttpProxy
import com.xuexiang.xhttp2.annotation.ThreadType
import com.xuexiang.xhttp2.model.ApiResult
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.activity_message_detail.*
import kotlinx.android.synthetic.main.layout_base_toolbar.*

class MessageDetailActivity : BaseActivity() {

    lateinit var msgPlayBean: MsgPlayBean
    private var msgId: String = ""
    private var mobile: String = ""
    private var mobileList: MutableList<Mobile> = mutableListOf()
    override fun getLayoutId(): Int = R.layout.activity_message_detail

    override fun initView(savedInstanceState: Bundle?) {
        toolbar_title.text = "信息详情"
        msgPlayBean = intent.getParcelableExtra("data")
        initData()
        changeState()
        message_detail_send.setOnClickListener {
            mobileList[0] = Mobile(mobile)
            val smsContent = message_detail_send_content.text.toString()
            val request = XHttp.custom()
            request.call(request.create(BaseApi.IMessage::class.java)
                    .smsSend(RequestBean(SendMsgInfo(mobileList, smsContent))))
                    .subscribeWith(object : TipRequestSubscriber<Map<String, String>?>() {
                        override fun onSuccess(apiResult: Map<String, String>?) {
                            apiResult?.let {
                                ToastUtils.toast("短信已发送")
                            }
                        }
                    })
        }
    }

    private fun initData() {
        if (msgPlayBean.receiveBean != null) {
            message_detail_sender.text = msgPlayBean.receiveBean!!.receiveName
            message_detail_Date.text = msgPlayBean.receiveBean!!.sendDate
            message_detail_num.text = msgPlayBean.receiveBean!!.receiveMobile
            message_detail_content.text = msgPlayBean.receiveBean!!.smsContent
            msgId = msgPlayBean.receiveBean!!.smsId
            mobile = msgPlayBean.receiveBean!!.receiveMobile
        } else {
            message_detail_sender.text = msgPlayBean.sendBean!!.sendName
            message_detail_Date.text = msgPlayBean.sendBean!!.sendDate
            message_detail_num.text = msgPlayBean.sendBean!!.sendMobile
            message_detail_content.text = msgPlayBean.sendBean!!.smsContent
            msgId = msgPlayBean.sendBean!!.smsId
            mobile = msgPlayBean.sendBean!!.sendMobile
        }

    }

    private fun changeState() {
        var params: MutableMap<String, String> = mutableMapOf()
        params["smsId"] = msgId
        val request = XHttp.custom()
        request.call(request.create(BaseApi.IMessage::class.java)
                .smsModifyStatus(RequestBean(params)))
                .subscribeWith(object : TipRequestSubscriber<Map<String, String>?>() {
                    override fun onSuccess(apiResult: Map<String, String>?) {
                        apiResult?.let {
                            ToastUtils.toast("状态更改成功")
                        }
                    }
                })
    }

}
